﻿using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using SqLiteEditor.Helper;
using SqLiteEditor.Properties;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace SqLiteEditor.Forms
{
    /// <summary>
    /// Interaction logic for FrmAddTable.xaml
    /// </summary>
    public partial class FrmAddTable : Window
    {
        ObservableCollection<TTabelStructure> ticketsList = new ObservableCollection<TTabelStructure>();

        public FrmAddTable()
        {
            InitializeComponent();

            InitializeSyntaxHighlighting();

            SqliteStructureDataGrid.ItemsSource = ticketsList;

            this.DataContext = this;
        }
               

        public string TableName
        {
            get { return (string)GetValue(TableNameProperty); }
            set { SetValue(TableNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TableName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TableNameProperty =
            DependencyProperty.Register("TableName", typeof(string), typeof(FrmAddTable), new PropertyMetadata("table_1", OnPropertyChanged));

        private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            FrmAddTable frmAddTable = (sender as FrmAddTable);
            frmAddTable.UpdateQuery();
        }

        private void UpdateQuery()
        {
            DMLEdittor.Text = SqliteHelper.Query_CreateTable(ticketsList.ToList(), TableName, Ch_Tbl_Exist.IsChecked.Value);
        }

        private void InitializeSyntaxHighlighting()
        {
            using (Stream s = TStreamHelper.OpenStream("Sql.xshd"))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    DMLEdittor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                }
            }

        }

        public string ResultQuery { get; set; }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            ResultQuery = DMLEdittor.Text;
            this.DialogResult = true;
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ResultQuery = "";
            this.DialogResult = false;
            Close();
        }

        private void Btn_GenerateQuery_Click(object sender, RoutedEventArgs e)
        {
            UpdateQuery();
        }
    }

}
